package com.github.rnewson.couchdb.lucene;

public class GenerationPoll implements AbstractGenerationPoll{
	Generation[] generationPoll = null;
	int sizepoll = 20;
	int currentsizepoll = 3;
	int currentposition = 0;
	int maxsizepoll = 5;
	String databasename = null;
	static GenerationPoll globalGenerationPoll = null;
	
	public GenerationPoll(int sizepoll)
	{
		this.sizepoll = sizepoll;
	}
	
	public AbstractGenerationPoll getGenerationPoll(String databasename)
	{
		return initGenerationPoll(databasename, this.sizepoll);
	}
//	static public GenerationPoll initGenerationPoll(String databasename)
//	{
//		if(globalGenerationPoll == null)
//		{
//			globalGenerationPoll = new GenerationPoll();
//			globalGenerationPoll.initializePoll(databasename);
//			return globalGenerationPoll;
//		}
//		else
//		{
//			return globalGenerationPoll;
//		}
//	}
	
	static private AbstractGenerationPoll initGenerationPoll(String databasename, int sizepoll)
	{
		if(globalGenerationPoll == null)
		{
			globalGenerationPoll = new GenerationPoll(sizepoll);
			globalGenerationPoll.initializePoll(databasename, sizepoll);
			return globalGenerationPoll;
		}
		else
		{
			return globalGenerationPoll;
		}
	}
	
	public void setSizePoll(int sizepoll)
	{
		this.sizepoll = sizepoll;
	}
	public int getSizePoll()
	{
		return this.sizepoll;
	}
	
//	private void initializePoll(String databasename)
//	{
//		initializePoll(databasename, maxsizepoll);
//	}
	
	protected void initializePoll(String databasename, int sizepoll)//初始化网页生成线程池，最新三个线程，并申请三个线程
	{
		this.databasename = databasename;
		if(sizepoll < 3)
			sizepoll = 3;
		generationPoll = new Generation[sizepoll];
		for(int i = 0; i < 3; i++)
		{
			generationPoll[i] = new Generation();
			generationPoll[i].initGeneration(this.databasename);
			Thread thread = new Thread(generationPoll[i]);
			
			generationPoll[i].setThread(thread);//设置generationPoll[currentsizepoll]所属的线程
			
			thread.start();
		}
		this.currentposition = 0;
		this.sizepoll = sizepoll;
		this.currentsizepoll = 3;
	}
	
	public AbstractGeneration getGeneration()
	{
		int travelNum = 0;
		//this.currentposition++;
		this.currentposition = this.currentposition % this.currentsizepoll;
		while(generationPoll[this.currentposition].getBusy() == true)
		{
			travelNum++;
			this.currentposition++;
			this.currentposition = this.currentposition % this.currentsizepoll;

			if(this.currentsizepoll < this.sizepoll &&
					travelNum >= this.currentsizepoll)
			{
				generationPoll[this.currentsizepoll] = new Generation();
				generationPoll[this.currentsizepoll].initGeneration(this.databasename);
				Thread thread = new Thread(generationPoll[currentsizepoll]);
				
				generationPoll[currentsizepoll].setThread(thread);//设置generationPoll[currentsizepoll]所属的线程
				
				thread.start();
				this.currentposition = this.currentsizepoll;
				this.currentsizepoll++;
			}
		}
		
		int position = currentposition;
		currentposition++;
		return generationPoll[position];
	}
	
	public void closeThread()//关闭网页生成线程，只留下3个
	{
		for(int i = 3; i < generationPoll.length; i++)
		{
			generationPoll[i].stopThread(true);
			generationPoll[i] = null;
		}
	}

	@Override
	public AbstractGeneration getGeneration(String databasename) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AbstractGenerationPoll getGenerationPoll() {
		// TODO Auto-generated method stub
		return null;
	}
}
